package com.jkl.controller.bsWorkflowLog;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jkl.comm.StringUtils.StringUtils;
import com.jkl.comm.service.FileService;
import com.jkl.entity.BaseResponseDTO;
import com.jkl.entity.bsAddition.BsAddition;
import com.jkl.entity.bsWorkflowLog.BsWorkflowLog;
import com.jkl.service.bsAddition.inter.BsAdditionServiceInterface;
import com.jkl.service.bsWorkflowLog.inter.BsWorkflowLogServiceInterface;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/jky/bsWorkflowLog")
@Slf4j
public class BsWorkflowLogController {

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private BsWorkflowLogServiceInterface bsWorkflowLogServiceInterface;
	@Autowired
	private BsAdditionServiceInterface bsAdditionServiceInterface;
	@Autowired
	private FileService FileService;

	@PostMapping("/getWorkflowLogList")
	public PageInfo<BsWorkflowLog> getWorkflowLogList(@RequestBody BsWorkflowLog bsWorkflowLog) {
		return bsWorkflowLogServiceInterface.getWorkflowLogList(bsWorkflowLog);
	}

	@PostMapping("/addWorkflowLog")
	public int addWorkflowLog(@RequestBody BsWorkflowLog bsWorkflowLog) {
		return bsWorkflowLogServiceInterface.insertBsWorkflowLog(bsWorkflowLog);
	}

	@PostMapping("/getbsWorkflowLogList")
	public BaseResponseDTO getIndexList(@RequestBody BsWorkflowLog bsWorkflowLog) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		List<BsWorkflowLog> bsWorkflowLogList = bsWorkflowLogServiceInterface.getSourceId(bsWorkflowLog);
		JSONObject returnJson = new JSONObject();
		JSONArray bsWorkflowLogArray = (JSONArray) JSONArray.toJSON(bsWorkflowLogList);
		returnJson.put("drIndexList", bsWorkflowLogArray);
		returnData.setReturnData(returnJson);
		return returnData;

	}

	@PostMapping("/savebsWorkflowLog")
	@Transactional(rollbackFor = Exception.class)
	public BaseResponseDTO save(HttpServletRequest request, HttpServletResponse response) {
		BaseResponseDTO returnData = new BaseResponseDTO();
		String bsWorkflowLogSave = request.getParameter("bsWorkflowLog");

		BsWorkflowLog bsWorkflowLog = JSON.parseObject(bsWorkflowLogSave, BsWorkflowLog.class);
		int drIndexInt = bsWorkflowLogServiceInterface.insertBsWorkflowLog(bsWorkflowLog);
		int bsWorkflowLogId = bsWorkflowLog.getRowId();
		if (drIndexInt != 0) {
			List<MultipartFile> multipartFile = null;
			String data = request.getParameter("bsAddition");
			// 将当前上下文初始化给 CommonsMutipartResolver （多部分解析器）
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
					request.getSession().getServletContext());
			if (multipartResolver.isMultipart(request)) {
				// 将request变成多部分request
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				multipartFile = multiRequest.getFiles("files");
			}
			BsAddition bsAddition = JSON.parseObject(data, BsAddition.class);
			for (int i = 0; i < multipartFile.size(); i++) {
				if (bsAddition != null) {
					bsAddition.setSourceId(bsWorkflowLogId);
					String name = "";
					String type = "";
					bsAddition.setSequence(i + 1);
					name = FileService.fileUpload(multipartFile.get(i));
					type = StringUtils.getExt(name);
					switch (type) {
					case "jpg":
						bsAddition.setAddType(1);
						break;
					case "mp3":
						bsAddition.setAddType(2);
						break;
					case "mp4":
						bsAddition.setAddType(3);
						break;
					default:
						bsAddition.setAddType(4);
						break;
					}
					bsAddition.setStatus(1);
					bsAddition.setAddUrl(name);
					BsAddition bsAdditionTemp = new BsAddition();
					bsAdditionTemp.setAddName(bsAddition.getAddName());
					bsAdditionTemp.setAddUrl(bsAddition.getAddUrl());
					bsAdditionTemp.setSourceId(bsAddition.getSourceId());
					bsAdditionTemp.setAddType(bsAddition.getAddType());
					bsAdditionTemp.setSequence(bsAddition.getSequence());
					bsAdditionTemp.setSourceTable(bsAddition.getSourceTable());
					bsAdditionTemp.setStatus(bsAddition.getStatus());
					int bsAdditionInt = bsAdditionServiceInterface.insert(bsAdditionTemp);
				}
			}

			returnData.setResultMessage("添加成功");
		} else {
			returnData.setResultMessage("添加失败");
		}
		return returnData;
	}

}
